//
//  TWBTabBar.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/12.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit

protocol TWBTabBarDelegate: NSObjectProtocol {
    
    func didSelectedComposeButton()
}

class TWBTabBar: UITabBar {
    
    var callBack: (()->())?
    
    weak var twbDelegate: TWBTabBarDelegate?
    
    private lazy var composeButton: UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: #selector(composeButtonAction), for: .touchUpInside)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    @objc private func composeButtonAction () {
        
        callBack?()
        
        twbDelegate?.didSelectedComposeButton()
    }
    
    private lazy var btn: UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .selected)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .selected)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: #selector(clickBtnAction), for: .touchUpInside)
        
        return btn
    }()
    
    @objc private func clickBtnAction () {
        
        callBack?()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        self.backgroundImage = #imageLiteral(resourceName: "tabbar_background")
        
        addSubview(btn)
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        btn.centerX = width / 2
        
        btn.centerY = height / 2
        
        let tabBarW = width / 5
        
        var index = 0
        
        for childView in subviews {
            
            if childView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                childView.width = tabBarW
                
                childView.x = CGFloat(index) * tabBarW
                
                index += 1
                
                if index == 2 {
                    index += 1
                }
                
            }
            
        }
        
    }
    
}
